package cn.lili.timetask.handler.impl.store;

import cn.lili.common.enums.SwitchEnum;
import cn.lili.modules.member.client.MemberEvaluationClient;
import cn.lili.modules.member.entity.dto.EvaluationQueryParams;
import cn.lili.modules.member.entity.vo.StoreRatingVO;
import cn.lili.modules.store.client.StoreClient;
import cn.lili.modules.store.entity.dos.Store;
import cn.lili.modules.store.entity.dto.StoreSearchParams;
import cn.lili.modules.store.entity.enums.StoreStatusEnum;
import cn.lili.modules.store.entity.params.StoreParams;
import cn.lili.timetask.handler.EveryDayExecute;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 店铺评分
 *
 * @author Chopper
 * @since 2021/3/15 5:30 下午
 */
@Component
@RequiredArgsConstructor
public class StoreRatingExecute implements EveryDayExecute {
    /**
     * 店铺
     */
    private final StoreClient storeClient;
    /**
     * 会员评价
     */
    private final MemberEvaluationClient memberEvaluationClient;


    @Override
    public void execute() {
        StoreSearchParams storeSearchParams = new StoreSearchParams();
        storeSearchParams.setStoreStatus(StoreStatusEnum.OPEN.name());
        //获取所有开启的店铺
        List<Store> storeList = storeClient.list(storeSearchParams);
        for (Store store : storeList) {
            //店铺所有开启的评价
            EvaluationQueryParams evaluationQueryParams = new EvaluationQueryParams();
            evaluationQueryParams.setStoreId(store.getId());
            evaluationQueryParams.setStatus(SwitchEnum.OPEN.name());
            StoreRatingVO storeRatingVO = memberEvaluationClient.getStoreRatingVO(evaluationQueryParams);

            if (storeRatingVO != null) {
                StoreParams storeParams = StoreParams.builder().storeId(store.getId())
                        .descriptionScore(storeRatingVO.getDescriptionScore())
                        .deliveryScore(storeRatingVO.getDeliveryScore())
                        .serviceScore(storeRatingVO.getServiceScore())
                        .build();
                //保存评分
                storeClient.updateScore(storeParams);
            }

        }


    }
}
